﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using SmlEngine.Graphics;
using SmlEngine.Extensions;
using SmlEngine.Sprites.Base;

namespace SmlEngine.Sprites.Base.WorldMap
{
    /// <summary>
    /// A tile for the world map.
    /// </summary>
    public class WorldMapTile : IWorldDrawable
    {
        #region Properties and Fields

        public WorldTile worldTile { get; private set; }
        public Vector2 location { get; private set; }

        public int id; // The id of this tile.  Set to -1 if custom.

        private bool isDataSet = false;
        #endregion

        /// <summary>
        /// Creates a new WorldMapTile.
        /// </summary>
        /// <param name="c_contentPath">The path to the tilesheet to use.</param>
        /// <param name="c_sourceRect">The area of the tilesheet which has the tile.</param>
        /// <param name="c_location">The location of the tile on the world map.</param>
        public WorldMapTile(string c_contentPath, Rectangle c_sourceRect, ContentManager cm)
        {
            worldTile = new WorldTile(c_contentPath, c_sourceRect.ToRectList());
            //LoadContent(null, cm);
        }

        /// <summary>
        /// Creates a new WorldMapTile from a file.
        /// </summary>
        /// <param name="gd">The GraphicsDevice to use.</param>
        /// <param name="c_filePath">The path to the graphics file.</param>
        /// <param name="c_sourceRect">The part of the image to use.</param>
        /// <param name="c_location">Where to draw the tile onscreen.</param>
        public WorldMapTile(GraphicsDevice gd, string c_filePath)
        {
            worldTile = new WorldTile(gd, null, c_filePath);
            //LoadContent(gd, null);
        }

        public WorldMapTile(GraphicsDevice gd, string c_filePath, Rectangle c_sourceRect)
        {
            worldTile = new WorldTile(c_filePath, c_sourceRect.ToRectList());
            //LoadContent(gd, null);
        }

        public WorldMapTile() { }

        public void LoadContent(GraphicsDevice gd, ContentManager cm)
        {
            worldTile.LoadContent(gd, cm, false);
        }

        public void UnloadContent()
        {
            worldTile.UnloadContent();
        }

        public void Update(GameTime gameTime) { }

        public void Draw(GameTime gameTime, SpriteBatch sb)
        {
            worldTile.Draw(gameTime, sb, location);
        }

        public WorldMapTile Clone()
        {
            WorldMapTile t = new WorldMapTile();
            t.worldTile = worldTile.Clone();
            t.location = location;
            t.id = id;
            return t;
        }

        public void SetData(Vector2 Position)
        {
            if (!isDataSet)
            {
                location = Position;
                isDataSet = true;
            }
        }
    }
}
